extends ../common

block content
  .container(style="margin-top:30px;")
    form#form.col-md-5.col-md-offset-3(method="post")
      .form-group
        label.sr-only(for="inputPassword") #{__('old password')}
        input#inputPassword.form-control(type="password" name="oldPassword" placeholder=`${__('old password')}` required)
      .form-group
        label.sr-only(for="inputNewPassword") #{__('new password')}
        input#inputNewPassword.form-control(type="password" name="newPassword" placeholder=`${__('new password')}` required)
      .form-group
        a#submitBtn.btn.btn-lg.btn-primary.btn-block #{__('Change Password')}

block js
  script().
    ensureLogin();

    var submit = false;
    $('#submitBtn').on('click', function () {
      if (submit) {
        return ;
      }
      submit = true;
      var accessToken = getAccessToken();
      var oldPassword = $('#inputPassword').val();
      var newPassword = $('#inputNewPassword').val();
      $.ajax({
        type: 'patch',
        data: JSON.stringify({ oldPassword: oldPassword, newPassword: newPassword }),
        contentType: 'application/json;charset=utf-8',
        headers: {
          Authorization : 'Bearer ' + accessToken,
        },
        url: '/users/password',
        dataType: 'json',
        success: function (data) {
          if (data.status == "OK") {
            alert("#{__('change success')}");
            logout();
          } else if (data.status == 401) {
            alert('token invalid');
            logout();
          } else {
            alert(data.message);
          }
          submit = false;
        }
      });
    });

